#ifndef DATABASE_H
#define DATABASE_H

#include <QObject>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QDateTime>
#include <QDebug>
#include <QStandardPaths>
#include <QDir>

struct Product {
    int id;
    QString name;
    QString category;
    int stock;
    double price;
    QString status;
    QDateTime createdTime;
    QDateTime updatedTime;
    
    Product() : id(0), stock(0), price(0.0) {}
    
    Product(int id, const QString& name, const QString& category, 
            int stock, double price, const QString& status,
            const QDateTime& createdTime = QDateTime::currentDateTime(),
            const QDateTime& updatedTime = QDateTime::currentDateTime())
        : id(id), name(name), category(category), stock(stock), 
          price(price), status(status), createdTime(createdTime), updatedTime(updatedTime) {}
};

class Database : public QObject
{
    Q_OBJECT

public:
    explicit Database(QObject *parent = nullptr);
    ~Database();
    
    // 数据库连接管理
    bool initializeDatabase();
    void closeDatabase();
    bool isConnected() const;
    
    // 商品数据操作
    bool addProduct(const Product& product);
    bool updateProduct(const Product& product);
    bool deleteProduct(int id);
    QList<Product> getAllProducts();
    Product getProductById(int id);
    
    // 数据库维护
    bool createTables();
    bool insertSampleData();
    QString getLastError() const;
    
private:
    QSqlDatabase m_database;
    QString m_lastError;
    
    QString getDatabasePath() const;
    void setLastError(const QString& error);
};

#endif // DATABASE_H